package com.cus.tom.transfer.sms;

import android.app.Activity;
import android.app.role.RoleManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Telephony;
import android.text.TextUtils;

import com.cus.tom.transfer.BuildConfig;

/* compiled from: ProGuard */
/* loaded from: classes2.dex */
public class a {

    /* renamed from: a, reason: collision with root package name */
    private static boolean f13682a = false;

    /* renamed from: b, reason: collision with root package name */
    private static boolean f13683b = false;

    public static boolean a(Context context) {
        if (Build.VERSION.SDK_INT >= 19) {
            try {
                Telephony.Sms.getDefaultSmsPackage(context);
                return true;
            } catch (NoSuchMethodError e) {
                e.printStackTrace();
            } catch (Throwable th) {
                th.printStackTrace();
                return false;
            }
        }
        return false;
    }

    public static boolean b(Context context) {
        try {
            String defaultSmsPackage = Telephony.Sms.getDefaultSmsPackage(context);
            if (TextUtils.isEmpty(defaultSmsPackage)) {
                defaultSmsPackage = BuildConfig.APPLICATION_ID;
            }
            return defaultSmsPackage.equalsIgnoreCase(context.getPackageName());
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        } catch (NoSuchMethodError e2) {
            e2.printStackTrace();
            return true;
        }
    }

    public static void d(Context context) {
        String str;
        try {
            str = Telephony.Sms.getDefaultSmsPackage(context);
        } catch (NoSuchMethodError e) {
            e.printStackTrace();
            str = "";
        }
        if (TextUtils.isEmpty(str)) {
            str = BuildConfig.APPLICATION_ID;
        }
        if (str.equalsIgnoreCase(context.getPackageName())) {
            return;
        }
        context.getSharedPreferences("SmsPermission", 0).edit().putString("D_S_F_A_4_4", str).apply();
    }

    public static void a(Activity activity, int i) {
        f13682a = true;
        try {
            if (Build.VERSION.SDK_INT > 28) {
                RoleManager roleManager = (RoleManager) activity.getSystemService("role");
                if (roleManager.isRoleAvailable("android.app.role.SMS") && !roleManager.isRoleHeld("android.app.role.SMS")) {
                    activity.startActivityForResult(roleManager.createRequestRoleIntent("android.app.role.SMS"), i);
                }
            } else {
                Intent intent = new Intent("android.provider.Telephony.ACTION_CHANGE_DEFAULT");
                intent.addFlags(32768);
                intent.putExtra("package", activity.getPackageName());
                activity.startActivityForResult(intent, i);
            }
        } catch (Throwable th) {
            th.printStackTrace();
            f13682a = false;
        }
    }

    public static boolean e(Context context) {
        f13682a = false;
        f13683b = true;
        try {
            String string = context.getSharedPreferences("SmsPermission", 0).getString("D_S_F_A_4_4", "");
            if (TextUtils.isEmpty(string)) {
                string = "com.android.mms";
            }
            if (Build.VERSION.SDK_INT > 28) {
                RoleManager roleManager = (RoleManager) ((Activity) context).getSystemService("role");
                if (roleManager.isRoleAvailable("android.app.role.SMS") && roleManager.isRoleHeld("android.app.role.SMS")) {
                    ((Activity) context).startActivity(((Activity) context).getPackageManager().getLaunchIntentForPackage(string));
                }
            } else {
                Intent intent = new Intent("android.provider.Telephony.ACTION_CHANGE_DEFAULT");
                intent.putExtra("package", string);
                if (!(context instanceof Activity)) {
                    intent.addFlags(268435456);
                }
                context.startActivity(intent);
            }
            return true;
        } catch (Throwable th) {
            th.printStackTrace();
            return false;
        }
    }

    public static boolean b(Activity activity, int i) {
        f13682a = false;
        f13683b = true;
        try {
            String string = activity.getSharedPreferences("SmsPermission", 0).getString("D_S_F_A_4_4", "");
            if (TextUtils.isEmpty(string)) {
                string = "com.android.mms";
            }
            if (Build.VERSION.SDK_INT > 28) {
                RoleManager roleManager = (RoleManager) activity.getSystemService("role");
                if (roleManager.isRoleAvailable("android.app.role.SMS") && roleManager.isRoleHeld("android.app.role.SMS")) {
                    activity.startActivityForResult(activity.getPackageManager().getLaunchIntentForPackage(string), i);
                }
            } else {
                Intent intent = new Intent("android.provider.Telephony.ACTION_CHANGE_DEFAULT");
                intent.putExtra("package", string);
                if (!(activity instanceof Activity)) {
                    intent.addFlags(268435456);
                }
                activity.startActivityForResult(intent, i);
            }
            return true;
        } catch (Throwable th) {
            th.printStackTrace();
            return false;
        }
    }
}